package com.gitee.simpleplus;

import java.io.Closeable;
import java.io.FileFilter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;

public class Conf implements Closeable{

	private String fixed;
	private String cust;
	private String dbDriver;
	private String hostPost;
	private String schemaName;
	private String springDatasourceUsername;
	private String springDatasourcePassword;
	private String springDatasourceUrl;
	private String groupId;
	private String artifactId;
	private String projectName;
	private String description;
	private String projectParentPath;
	private String serverPort;
	private String excludeDirs;
	private String version;
	private SwaggerConf swagger;
	
	private Connection conn;
	
	public String getFixed() {
		return fixed;
	}

	public void setFixed(String fixed) {
		this.fixed = fixed;
	}

	public String getCust() {
		return cust;
	}

	public void setCust(String cust) {
		this.cust = cust;
	}

	public String getDbDriver() {
		return dbDriver;
	}

	public void setDbDriver(String dbDriver) {
		this.dbDriver = dbDriver;
	}

	public String getHostPost() {
		return hostPost;
	}

	public void setHostPost(String hostPost) {
		this.hostPost = hostPost;
	}

	public String getSchemaName() {
		return schemaName;
	}

	public void setSchemaName(String schemaName) {
		this.schemaName = schemaName;
	}

	public String getSpringDatasourceUsername() {
		return springDatasourceUsername;
	}

	public void setSpringDatasourceUsername(String springDatasourceUsername) {
		this.springDatasourceUsername = springDatasourceUsername;
	}

	public String getSpringDatasourcePassword() {
		return springDatasourcePassword;
	}

	public void setSpringDatasourcePassword(String springDatasourcePassword) {
		this.springDatasourcePassword = springDatasourcePassword;
	}

	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public String getArtifactId() {
		return artifactId;
	}

	public void setArtifactId(String artifactId) {
		this.artifactId = artifactId;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getProjectParentPath() {
		return projectParentPath;
	}

	public void setProjectParentPath(String projectParentPath) {
		this.projectParentPath = projectParentPath;
	}

	public String getSpringDatasourceUrl() {
		return springDatasourceUrl;
	}

	public void setSpringDatasourceUrl(String springDatasourceUrl) {
		this.springDatasourceUrl = springDatasourceUrl;
	}
	
	public String getServerPort() {
		return serverPort;
	}

	public void setServerPort(String serverPort) {
		this.serverPort = serverPort;
	}

	public String getExcludeDirs() {
		return excludeDirs;
	}

	public void setExcludeDirs(String excludeDirs) {
		this.excludeDirs = excludeDirs;
	}
	

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public SwaggerConf getSwagger() {
		return swagger;
	}

	public void setSwagger(SwaggerConf swagger) {
		this.swagger = swagger;
	}

	public void init(){
		get();
	}

	public Connection get(){
		if(conn == null){
			springDatasourceUrl = String.format("jdbc:mysql://%s/%s?useSSL=false&useUnicode=true&characterEncoding=utf-8", hostPost, schemaName);
			try {
				Class.forName(dbDriver);
				conn =DriverManager.getConnection(
						springDatasourceUrl, springDatasourceUsername, springDatasourcePassword);
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
		
		return conn;
	}

	@Override
	public void close() throws IOException {
		if(conn != null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public String toString() {
		ToStringBuilder builder = new ToStringBuilder(this);
		builder.append("fixed", fixed);
		builder.append("cust", cust);
		builder.append("dbDriver", dbDriver);
		builder.append("hostPost", hostPost);
		builder.append("schemaName", schemaName);
		builder.append("springDatasourceUsername", springDatasourceUsername);
		builder.append("springDatasourcePassword", springDatasourcePassword);
		builder.append("springDatasourceUrl", springDatasourceUrl);
		builder.append("groupId", groupId);
		builder.append("artifactId", artifactId);
		builder.append("projectName", projectName);
		builder.append("description", description);
		builder.append("projectParentPath", projectParentPath);
		builder.append("serverPort", serverPort);
		builder.append("excludeDirs", excludeDirs);
		builder.append("version", version);
		builder.append("swagger", swagger);
		return builder.toString();
	}

	public String getTargetFileAbsoluteDir() {
		return projectParentPath + projectName;
	}

	public FileFilter findFileFilter() {
		String[] strs = Optional.ofNullable(StringUtils.split(excludeDirs, ",")).orElse(new String[0]);
		return f->{
			for(String s : strs){
				if(!StringUtils.isEmpty(s) && f.getAbsolutePath().indexOf(s) > -1){
					return false;
				}
			}
			return f.getName().endsWith(".ftl");
		};
	}
	
	
	public class SwaggerConf{
		private String basePackage;
		private String title;
		private String description;
		private String termsOfServiceUrl;
		private Contact contact;
		public String getBasePackage() {
			return basePackage;
		}
		public void setBasePackage(String basePackage) {
			this.basePackage = basePackage;
		}
		public String getTitle() {
			return title;
		}
		public void setTitle(String title) {
			this.title = title;
		}
		public String getDescription() {
			return description;
		}
		public void setDescription(String description) {
			this.description = description;
		}
		public String getTermsOfServiceUrl() {
			return termsOfServiceUrl;
		}
		public void setTermsOfServiceUrl(String termsOfServiceUrl) {
			this.termsOfServiceUrl = termsOfServiceUrl;
		}
		public Contact getContact() {
			return contact;
		}
		public void setContact(Contact contact) {
			this.contact = contact;
		}
		@Override
		public String toString() {
			ToStringBuilder builder = new ToStringBuilder(this);
			builder.append("basePackage", basePackage);
			builder.append("title", title);
			builder.append("description", description);
			builder.append("termsOfServiceUrl", termsOfServiceUrl);
			builder.append("contact", contact);
			return builder.toString();
		}
		
		public class Contact {
			private String name;
			private String url;
			private String email;

			public Contact() {
			}

			public void setName(String name) {
				this.name = name;
			}

			public void setUrl(String url) {
				this.url = url;
			}

			public void setEmail(String email) {
				this.email = email;
			}

			public String getName() {
				return name;
			}

			public String getUrl() {
				return url;
			}

			public String getEmail() {
				return email;
			}

			@Override
			public String toString() {
				ToStringBuilder builder = new ToStringBuilder(this);
				builder.append("name", name);
				builder.append("url", url);
				builder.append("email", email);
				return builder.toString();
			}
			
			
		}
		
	}
}
